﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace mpsalaryprojectgroup
{
    public partial class NEWNEWSignup : System.Web.UI.Page
    {
        String username = "";
        String loginName = "";
        protected void Page_Load(object sender, EventArgs e)
        {


            if (Session["Usename"] != null)
            {
                username = (string)(Session["Usename"]);
                if (username == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = username;
                }
            }
            else if (Session["UsenameLogin"] != null)
            {
                loginName = (string)(Session["UsenameLogin"]);
                if (loginName == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = loginName;
                }
            }
            else
            {
                Response.Redirect("NEWMainPage.aspx");
            }

            CompareValidator1.ValueToCompare = DateTime.Today.ToShortDateString();
            
            ddlSchool.Visible = false;
            tbxCompany.Visible = false;
            lblSchool.Visible = false;
            lblCompany.Visible = false;
            lblGender.Visible = false;

            //this is to retrieve information from the mainpage when they sign up
            if (!Page.IsPostBack)
            {
                //sign up
                String usename = (string)(Session["Usename"]);
                String emailaddress = (string)(Session["Email"]);
               // String pw = (string)(Session["PasswordSignUp"]);
                //mpsalary.App_Code.User user = new mpsalary.App_Code.User();
                lblUsername.Text = usename;
                tbxUsername.Text = usename;
                tbxEmail.Text = emailaddress;
                //tbxCurrentPassword.Text = pw;

                ////log in
                //String usenameLogin = (string)(Session["UsernameLogin"]);
                //String passwordLogin = (string)(Session["Password"]);
                //mpsalary.App_Code.User user1 = new mpsalary.App_Code.User();
                //user1.RetrieveUserInfo();
                //user1.Salutation = ddlSalutation.Text;
                //user1.School = ddlSchool.Text;
                //user1.Usename = tbxUsername.Text;
                //user1.Email = tbxEmail.Text;
                //user1.Password = tbxCurrentPassword.Text;
                //user1.Company = tbxCompany.Text;
                //user1.FirstName = tbxFirstName.Text;
                //user1.LastName = tbxLastName.Text;
                //user1.MobileNo = tbxMobileNo.Text;
                //user1.NRIC = tbxNRIC.Text;
                //user1.Occupation = rblOccupation.Text;

            }

            //drop down list selected value
            if (ddlSalutation.SelectedValue.Equals("Ms"))
            {
                lblGender.Text = "Female";
            }
            else if (ddlSalutation.SelectedValue.Equals("Mrs"))
            {
                lblGender.Text = "Female";
            }
            else if (ddlSalutation.SelectedValue.Equals("Mr"))
            {

                lblGender.Text = "Male";
            }
            else if (ddlSalutation.SelectedValue.Equals("Mdm"))
            {

                lblGender.Text = "Female";
            }


             if(Page.IsPostBack)
            {
                //if user declare as student, company textbox will be unenabled
                //they don't need to key in anything in the textbox
                if (rblOccupation.SelectedValue.Equals("Student"))
                {
                    tbxCompany.Visible = false;
                    lblCompany.Visible = false;
                    lblSchool.Visible = true;
                    ddlSchool.Visible = true;
                    //CompanyValidator.Visible = false;


                }
                //if user declare as a employee, student textbox will be unenabled
                //they don't need to key in anything in the textbox
                else if (rblOccupation.SelectedValue.Equals("Employer"))
                {
                    ddlSchool.Visible = false;
                    lblSchool.Visible = false;
                    tbxCompany.Visible = true;
                    lblCompany.Visible = true;
                    //SchoolValidator.Visible = false;
                }

                else if (rblOccupation.SelectedValue.Equals("Others"))
                {
                    lblSchool.Visible = false;
                    lblCompany.Visible = false;
                    ddlSchool.Visible = false;
                    //SchoolValidator.Visible = false;
                    tbxCompany.Visible = false;
                    //CompanyValidator.Visible = false;
                }
                
            }
             DisableBufferingOnPage();
        }

        protected void DisableBufferingOnPage()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            // set expiry date in the past
            Response.Expires = -1;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Expires = -1;
            Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

      
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //String password = (string)(Session["Password"]);
            //tbxCurrentPassword.Text = password;

            lblChange.Text = "";
            lblnotSame.Text = "";
            lblIncorrect.Text = "";
            lblUpdateStatus.Text = "";
            
            mpsalary.App_Code.User user = new mpsalary.App_Code.User();

            user.Occupation = rblOccupation.SelectedValue;
            user.Usename = tbxUsername.Text;
            user.FirstName = tbxFirstName.Text;
            user.LastName = tbxLastName.Text;
            user.School = ddlSchool.Text;
            user.Company = tbxCompany.Text;
            user.Salutation = ddlSalutation.Text;
            user.Gender = lblGender.Text;
            user.NRIC = tbxNRIC.Text;
            user.Email = tbxEmail.Text;
            user.DOB = datepicker.Text;
            ////for current password
            ////user.Password = password;
            //user.ChangePw = tbxChangePassword.Text;
            //user.ChangeNewPassword = tbxNewChangePassword.Text;
            user.MobileNo = tbxMobileNo.Text;

            user.UpdateUser();
            lblUpdateStatus.Text = " Your Account has been updated successfully";

            if (Page.IsPostBack)
            {

                user.RetrievePassword();
                if (tbxCurrentPassword.Text == user.Password)
                {
                    if (tbxChangePassword.Text == tbxNewChangePassword.Text)
                    {
                        
                        user.Password = tbxNewChangePassword.Text;
                        user.ChangePassword();
                        if (tbxCurrentPassword.Text != "" && tbxChangePassword.Text != "" && tbxNewChangePassword.Text != "")
                        {
                            lblChange.Text = "Password changed successfully";
                            lblUpdateStatus.Text = "Password changed successfully";

                            MailMessage email = new MailMessage();
                            email.From = new MailAddress("Fromthepgsteam@gmail.com", "PGS Portal");
                            email.To.Add(new MailAddress(tbxEmail.Text));
                            email.Subject = "PGS Portal- Change of Password";
                            email.Body = "<html><body><b>Dear " + tbxUsername.Text + ",</b><br><br>Your password has been changed at PGS Portal.<br><br>If you did not make this change,please email us at thepgsteam@gmail.com on how to resecure your account.<br><br><br><b>Yours sincerely,<br> The PGS Team<br><br></b> </body></html>";
                            email.IsBodyHtml = true;
                            email.Priority = MailPriority.Normal;

                            SmtpClient mailclient = new SmtpClient("smtp.gmail.com");
                            mailclient.Credentials = new System.Net.NetworkCredential("thepgsteam@gmail.com", "thepgsteam");
                            mailclient.Port = 587;
                            mailclient.EnableSsl = true;
                            mailclient.Send(email);


                        }
                    }
                    else
                    {
                        
                        if (tbxCurrentPassword.Text != "" && tbxChangePassword.Text != "" && tbxNewChangePassword.Text != "")
                        {
                            lblnotSame.Text = "Password does not match";
                            lblUpdateStatus.Text = "Password does not match";
                        }
                    }
                }
                
                    
                else if (tbxCurrentPassword.Text != "" && tbxChangePassword.Text != "" && tbxNewChangePassword.Text != "")
                    {
                        lblIncorrect.Text = "Password incorrect";
                        lblUpdateStatus.Text = "Password incorrect";
                    }
                else if (tbxCurrentPassword.Text == "" && tbxChangePassword.Text != "" && tbxNewChangePassword.Text != "")
                {
                    lblIncorrect.Text = "Please enter your current password.";
                    lblUpdateStatus.Text = "Please enter your current password.";
                }
              

            }

        }

            

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            tbxEmail.Enabled = true;
        }

       
        }

    }